﻿using UnityEngine;
using UnityEditor;

namespace HKTools
{
    /// <summary>
    /// HK_RotTarget 的 Inspector 编辑器拓展
    /// </summary>
    [CanEditMultipleObjects]
    [CustomEditor(typeof(HK_RotTarget))]
    public class HK_RotTargetEditor : Editor
    {
        float value = 0;
        Transform camTemp_TF;

        public override void OnInspectorGUI()
        {
            GUILayout.Space(5);

            if (GUILayout.Button("根据物体名称，添加 Target 类型"))
            {
                HK_AddTargetEnumValue.AddTargetType((HK_BaseTarget)target);
            }

            GUILayout.Space(10);

            // 更新序列化对象  
            serializedObject.Update();

            EditorGUILayout.PropertyField(serializedObject.FindProperty("targetType"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty("FOV"));

            EditorGUILayout.PropertyField(serializedObject.FindProperty("distance"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty("minDis"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty("maxDis"));

            EditorGUILayout.PropertyField(serializedObject.FindProperty("height"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty("length"));

            EditorGUILayout.PropertyField(serializedObject.FindProperty("leftRotLimit"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty("rightRotLimit"));

            // 应用修改  
            serializedObject.ApplyModifiedProperties();

            HK_RotTarget targetItem = target as HK_RotTarget;

            GUILayout.Space(10);

            if (GUILayout.Button("生成测试Camera"))
            {
                if (targetItem.transform.childCount > 0)
                    return;

                GameObject camTemp = new GameObject();
                camTemp.name = "Test Cam";
                camTemp.AddComponent<Camera>();

                camTemp_TF = camTemp.transform;
                camTemp_TF.SetParent(targetItem.transform);
                camTemp_TF.position = targetItem.transform.position + targetItem.transform.forward * targetItem.distance;
                camTemp_TF.LookAt(targetItem.transform);

                camTemp_TF.GetComponent<Camera>().fieldOfView = targetItem.FOV;

                value = (targetItem.distance - targetItem.minDis) / (targetItem.maxDis - targetItem.minDis);
            }

            if (GUILayout.Button("删除测试Camera"))
            {
                if (targetItem.transform.childCount <= 0)
                    return;

                DestroyImmediate(targetItem.transform.GetComponentInChildren<Camera>().gameObject);
                camTemp_TF = null;
            }

            if (camTemp_TF != null)
            {
                GUILayout.Space(5);

                value = GUILayout.HorizontalSlider(value, 0, 1);

                camTemp_TF.position = targetItem.transform.position +
                    targetItem.transform.forward * (targetItem.minDis + (targetItem.maxDis - targetItem.minDis) * value);

                GUILayout.Space(15);
            }
        }
    }
}